from django.db import models

# Create your models here.
# 1、环境表
# name:venData
# 字段：id，venname,url，DB，DBuser,DBpwd,DBprot, isdelete, create_time, create_user,updata_time, updata_user
#


class Vendata(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, null=False, help_text='环境名称')
    url = models.CharField(max_length=200, null=True, help_text='环境域名')
    db = models.CharField(max_length=200, null=True, help_text='数据库服务域名')
    dbuser = models.CharField(max_length=100, null=True, help_text='数据库账号')
    dbpwd = models.CharField(max_length=100, null=True, help_text='数据库密码')
    dbport = models.CharField(max_length=20, null=True, help_text='数据库端口')
    dbname = models.CharField(max_length=100, null=True, help_text='数据库端口')
    global_toke = models.TextField(null=True, help_text='token')
    resapi = models.CharField(max_length=100, null=True, help_text="返回密钥的接口")
    isdelete = models.IntegerField(default=0, null=True, help_text='是否已删，0为否，1为是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'vendata'

    def __str__(self):
        return '名称：%s\t域名：%s\tDB域名：%s\t' %(self.name, self.url, self.db)

class EnvdbConfigdata(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, null=False, help_text='数据库标识名称')
    parname = models.CharField(max_length=50, null=False, help_text='单个数据全局变量名称，数据环境时必须填写')
    db = models.CharField(max_length=200, null=True, help_text='数据库服务域名')
    dbuser = models.CharField(max_length=100, null=True, help_text='数据库账号')
    dbpwd = models.CharField(max_length=100, null=True, help_text='数据库密码')
    dbport = models.CharField(max_length=20, null=True, help_text='数据库端口')
    dbname = models.CharField(max_length=100, null=True, help_text='数据库端口')
    type = models.IntegerField(null=True, help_text='是否已删，1为mysql，2为oracle，3为多维数据库')
    envid = models.ForeignKey('Vendata', on_delete=models.DO_NOTHING)
    isdelete = models.IntegerField(default=0, null=True, help_text='是否已删，0为否，1为是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'envdbconfigdata'

    def __str__(self):
        return '名称：%s\t数据库环境：%s\t账号：%s\t' % (self.name, self.db, self.dbuser)


#
# 2、接口表
# name:apiData
# 字段：id, name，api，headers，body，response, isMock，create_time，create_user, isdelete, updata_time, updata_user
#

class Apidata(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, null=False, help_text='接口名称')
    catid = models.ForeignKey('Apicat', on_delete=models.DO_NOTHING)
    api = models.TextField(null=True, help_text='接口路径')
    apimethod = models.CharField(max_length=200, null=True, help_text='接口类型,POST,GET')
    headers = models.TextField(null=True, help_text='请求头')
    body = models.TextField(null=True, help_text='请求参数')
    response = models.TextField(null=True, help_text='返回数据')
    ismock = models.IntegerField(default=0, null=True, help_text='是否mock, 0为否，1为是')
    isdelete = models.IntegerField(default=0, null=True, )
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'apidata'

    def __str__(self):
        return '{"接口名":"%s","接口路径":"%s", "MOCK": %d}' % (self.name, self.api, self.ismock)

class Apioldata(models.Model):
    id = models.AutoField(primary_key=True)
    oldid = models.IntegerField(null=True, help_text='Apidata的id')
    name = models.CharField(max_length=100, null=False, help_text='接口名称')
    catid = models.IntegerField(null=True, help_text='Apicate的id')
    api = models.CharField(max_length=500, null=True, help_text='接口路径')
    apimethod = models.TextField(null=True, help_text='接口类型,POST,GET')
    headers = models.TextField(null=True, help_text='请求头')
    body = models.TextField(null=True, help_text='请求参数')
    response = models.TextField(null=True, help_text='返回数据')
    ismock = models.IntegerField(default=0, null=True, help_text='是否mock, 0为否，1为是')
    isdelete = models.IntegerField(default=0, null=True, )
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'apioldata'

    def __str__(self):
        return '{"接口名":"%s","接口路径":"%s", "MOCK": %d}' % (self.name, self.api, self.ismock)



# 3、用例表
# name:caseData
# 字段：id，
#           name,
#           api,
#           headers,
#           body,
#           checkmethod：检查数据方式
#           checkdata：检查字段或方案
#           datainterception：参数化的方式
#           create_time
#           create_user
#           isdelete,
#           updata_time,
#           updata_user

class Casedata(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, null=False, help_text='用例名称')
    catid = models.ForeignKey('Casecat', on_delete=models.DO_NOTHING)
    api = models.CharField(max_length=200, null=True, help_text='接口路径')
    apimethod = models.CharField(max_length=200, null=True, help_text='接口类型,POST,GET')
    headers = models.TextField(null=True, help_text='请求头')
    body = models.TextField(null=True, help_text='请求参数')
    checkrestype = models.IntegerField(null=True, help_text='检查数据类型 0是返回头，1是返回数据，2是接口状态')
    checkmethod = models.CharField(max_length=50, null=True, help_text='返回检查方式')
    checkdata = models.CharField(max_length=200, null=True, help_text='检查点')
    comments = models.TextField(null=True, help_text='用户备注')
    isdelete = models.IntegerField(default=0, null=True, help_text='是否已删， 0 未删， 1 已删')
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'casedata'

    def __str__(self):
        return '用例名%s\t接口路径%s\t' % (self.name, self.api)




#
# 4，用例集合（计划表）
# name:casePlan
# 字段：id
#           case_ids：用例id池，已经用到的用例，不可以删除
#           venid：环境表的id外键，如果使用某环境id，venData表的对应环境不可删除
#           create_time
#           create_user
#           isdelete,
#           updata_time,
#           updata_user

class Caseplan(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, null=False, help_text='用例集名称')
    venid = models.ForeignKey('Vendata', null=True, on_delete=models.DO_NOTHING)
    exctime = models.CharField(max_length=100, null=True, help_text='执行时间')
    sched = models.IntegerField(default=0, null=True, help_text='是否触发定时任务，0否，1是')
    qymsg = models.IntegerField(null=True, help_text='0,不发送消息， 1，发送消息')
    webhook = models.CharField(max_length=500, null=True, help_text='企业微信群机器人webhook')
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'caseplan'

    def __str__(self):
        return '用例集名称：%s\t' % (self.name)


class PlanlinkCase(models.Model):
    id = models.AutoField(primary_key=True)
    planid = models.ForeignKey('Caseplan', on_delete=models.DO_NOTHING)
    caseid = models.ForeignKey('Casedata', on_delete=models.PROTECT)
    index = models.IntegerField(null=True, help_text="用例排序")
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True)
    updata_user = models.IntegerField(null=True)
    class Meta:
        managed = True
        db_table = 'planlinkcase'
        verbose_name = '计划用例关联'


class Prvbefcase(models.Model):
    id = models.AutoField(primary_key=True)
    caseid = models.ForeignKey('Casedata', on_delete=models.PROTECT, null=False, help_text='用例的id,外键')
    # index = models.IntegerField(null=False, help_text='顺序，基于caseid排序，顺序里如果用例case在中间，用例也占个排序，前面的是前置，后面的是后置')
    method = models.CharField(max_length=200, null=False, help_text='公共方法')
    f_params = models.TextField(null=True, help_text='参数1')
    s_params = models.TextField(null=True, help_text='参数2')
    t_params = models.TextField(null=True, help_text='参数3')
    result = models.CharField(max_length=200, null=True, help_text='变量名称')
    preorbef = models.IntegerField(null=True, help_text='前置或后置，0：前置自动定义，1是后置自定义，3、是操作数据库，4是参数化')
    isdelete = models.IntegerField(default=0, null=False)
    create_time = models.DateTimeField(auto_now_add=True)
    create_user = models.IntegerField(null=True)
    updata_time =models.DateTimeField(auto_now=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'prvbefcase'

    def __str__(self):
        return '公共方法名称：%s\t' % (self.common)


class PlanExRecord(models.Model):
    id = models.AutoField(primary_key=True)
    planid = models.ForeignKey('Caseplan', null=True,on_delete=models.DO_NOTHING)
    excutnum = models.CharField(max_length=200, null=False, help_text='执行批次编号，按计划+用例生成数据，用此字段判断报告文件集，有多个相同编号的数据组成一封报告')
    planName = models.CharField(max_length=200, null=True, help_text='计划名称')
    caseCount = models.IntegerField(null=True, help_text='用例总数')
    passCount = models.IntegerField(null=True, help_text='通过数量')
    failCount = models.IntegerField(null=True, help_text='不通过数量')
    totalTime = models.CharField(max_length=100, null=True, help_text='计划执行总用时')
    thrCount = models.IntegerField(null=True, help_text='300ms<=s<700ms的数量')
    sevCount = models.IntegerField(null=True, help_text='700ms<=s<1000ms的数量')
    secCount = models.IntegerField(null=True, help_text='1000ms<=s的数量')
    isdelete = models.IntegerField(null=False, default=0, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed=True
        db_table = 'planexrecord'

    def __str__(self):
        return '编号%s' % (str(self.excutnum))


class Reportdata(models.Model):
    id = models.AutoField(primary_key=True)
    casename = models.CharField(max_length=100, null=True, help_text="存放用例名称")
    excutnum = models.CharField(max_length=200, null=False, help_text='执行批次编号，按计划+用例生成数据，用此字段判断报告文件集，有多个相同编号的数据组成一封报告')
    planid = models.ForeignKey('Caseplan', null=True,on_delete=models.DO_NOTHING)
    caseid = models.ForeignKey('CaseData', on_delete=models.DO_NOTHING)
    testresult = models.TextField(null=False, help_text='测试报告展示文案，url + header +body +response + check + 参数化，格式[{},{},{}....]')
    status = models.IntegerField(null=False, help_text='执行结果状态 ，1是成功，0是失败')
    elapsedt = models.CharField(max_length=100, null=True, help_text='请求响应时间')
    isdelete = models.IntegerField(null=False, default=0, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)


    class Meta:
        managed=True
        db_table = 'reportdata'

    def __str__(self):
        return '编号%s' % (str(self.excutnum))


"""
接口归类管理Category表  apicat
"""
class Apicat(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200, null=False, help_text='node名称')
    parentid = models.IntegerField(null=True, help_text='父节点')
    level = models.IntegerField(null=True, help_text='节点等级,暂时最多三级,代码控制')
    isdelete = models.IntegerField(default=0,null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed=True
        db_table = 'apicat'

    def __str__(self):
        return '{"id":%d, "name":"%s", "parentid":%d, "level":%d}' % (self.id, str(self.name), self.parentid, self.level)

"""
用例归类管理Category表 casecat
"""
class Casecat(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200, null=False, help_text='node名称')
    parentid = models.IntegerField(null=True, help_text='父节点')
    level = models.IntegerField(null=True, help_text='节点等级,暂时最多三级,代码控制')
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed=True
        db_table = 'casecat'

    def __str__(self):
        return '{id:%d, name:%s, parentid:%d, level:%d}' % (self.id, str(self.name), self.parentid, self.level)


class Apimethod(models.Model):
    id = models.AutoField(primary_key=True)
    method = models.CharField(max_length=30, null=False, help_text='接口类型')
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed=True
        db_table = 'apimethod'

    def __str__(self):
        return '{id:%d, method:%s}' % (self.id, str(self.method))


class Commonmethod(models.Model):
    id = models.AutoField(primary_key=True)
    method_name = models.CharField(max_length=100, null=False, help_text='公共方法名称')
    function = models.CharField(max_length=100, null=False, help_text='方法名称')
    method_tpye = models.IntegerField(default=0, null=False, help_text='0 自定义方法， 1校验方法，2 参数化， -1 系统方法')
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed=True
        db_table = 'commonmethod'

    def __str__(self):
        return '{id:%d, method_name:%s, function:%s}' % (self.id, str(self.method_name), str(self.function))

class SchedulData(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, null=True, help_text='计划名称')
    job_id = models.IntegerField(null=True, help_text='某些任务原因的id,可能有重复，需要联合任务类型判断')
    schtime = models.CharField(max_length=100, null=True, help_text='执行时间，格式：年  月  日  时  分  秒，*号代表为空')
    params = models.CharField(max_length=500, null=True, help_text='定时任务需要的参数，list格式的字符串')
    jobtype = models.IntegerField(null=True, help_text='定时任务类型，1 swagger数据集成，2 测试计划执行')
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed=True
        db_table = 'scheduldata'

    def __str__(self):
        return '{id:%d, name:%s, schtime:%s, params:%s}' % (self.id, str(self.name), str(self.schtime), str(self.params))

#用例导入记录
class ImportRecord(models.Model):
    id = models.AutoField(primary_key=True)
    viewname = models.CharField(max_length=200, null=True, help_text='展示名称')
    savename = models.CharField(max_length=200, null=True, help_text='实际文件名称')
    casecount = models.IntegerField(null=True, help_text='导入用例数量')
    succount = models.IntegerField(null=True, help_text='导入成功数量')
    filcount = models.IntegerField(null=True, help_text='导入失败数量')
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'importrecord'

    def __str__(self):
        return '{id:%d, viewname:%s, casecount:%s, succount:%s, filcount:%s}' \
               % (self.id, str(self.viewname), str(self.casecount), str(self.succount), str(self.filcount))


class Commethoddes(models.Model):
    id=models.AutoField(primary_key=True)
    title=models.CharField(max_length=200, null=True, help_text='方法标题')
    result=models.CharField(max_length=200, null=True, help_text='变量名')
    f_parames=models.CharField(max_length=200, null=True, help_text='第一个参数描述')
    s_parames=models.CharField(max_length=200, null=True, help_text='第二个参数描述')
    t_parames = models.CharField(max_length=200, null=True, help_text='第三个参数描述')
    descript=models.TextField(null=True,help_text="方法规则描述")
    isdelete = models.IntegerField(default=0, null=True, help_text='是否删除， 0否，1是')
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    create_user = models.IntegerField(null=True)
    updata_time = models.DateTimeField(auto_now=True, null=True)
    updata_user = models.IntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'commethoddes'

    def __str__(self):
        return '{id:%d, title:%s, result:%s, f_parames:%s, s_parames:%s, t_parames:%s}' \
               % (self.id, str(self.title), str(self.result), str(self.f_parames), str(self.s_parames), str(self.t_parames))